package com.summerbird.mallchat.common.chat.dao;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.summerbird.mallchat.common.chat.domain.entity.Contact;
import com.summerbird.mallchat.common.chat.domain.entity.Message;
import com.summerbird.mallchat.common.chat.domain.vo.request.ChatMessageReadReq;
import com.summerbird.mallchat.common.chat.mapper.ContactMapper;
import com.summerbird.mallchat.common.common.constant.RedisKey;
import com.summerbird.mallchat.common.common.domain.vo.req.CursorPageBaseReq;
import com.summerbird.mallchat.common.common.domain.vo.resp.CursorPageBaseResp;
import com.summerbird.mallchat.common.common.utils.CursorUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 会话列表 服务实现类
 * </p>
 *
 * @author <a href="https://github.com/zongzibinbin">abin</a>
 * @since 2023-07-16
 */
@Service
public class ContactDao extends ServiceImpl<ContactMapper, Contact> {

    /**
     * 更新所有人的会话时间，没有就直接插入
     */
    public void refreshOrCreateActiveTime(Long roomId, List<Long> memberUidList, Long msgId, Date activeTime) {
        baseMapper.refreshOrCreateActiveTime(roomId, memberUidList, msgId, activeTime);
    }

    public Contact get(Long receiveUid, Long roomId) {
        return lambdaQuery()
                .eq(Contact::getRoomId, roomId)
                .eq(Contact::getUid, receiveUid)
                .one();
    }

    /**
     * 获取用户会话列表
     */
    public CursorPageBaseResp<Contact> getContactPage(Long uid, CursorPageBaseReq request) {
        return CursorUtils.getCursorPageByMysql(this, request, wrapper -> {
            wrapper.eq(Contact::getUid, uid);
        }, Contact::getActiveTime);
    }

    public CursorPageBaseResp<Contact> getReadPage(Message message, ChatMessageReadReq request) {
        return CursorUtils.getCursorPageByMysql(this, request, wrapper -> {
            wrapper.ne(Contact::getUid, message.getFromUid());// 过滤自己的消息
            wrapper.eq(Contact::getRoomId, message.getRoomId());
            wrapper.ge(Contact::getActiveTime, message.getCreateTime());// 未读：大于该消息时间线
        }, Contact::getReadTime);
    }

    public CursorPageBaseResp<Contact> getUnReadPage(Message message, ChatMessageReadReq request) {
        return CursorUtils.getCursorPageByMysql(this, request, wrapper -> {
            wrapper.ne(Contact::getUid, message.getFromUid());// 过滤自己的消息
            wrapper.eq(Contact::getRoomId, message.getRoomId());
            wrapper.lt(Contact::getActiveTime, message.getCreateTime());// 已读：小于该消息时间线
        }, Contact::getReadTime);
    }

    public Integer getTotalCount(Long roomId) {
        return lambdaQuery()
                .eq(Contact::getRoomId, roomId)
                .count();
    }

    public Integer getReadCount(Message message) {
        return lambdaQuery()
                .eq(Contact::getRoomId, message.getRoomId())
                .ne(Contact::getUid, message.getFromUid()) // 排除自己
                .ge(Contact::getReadTime, message.getCreateTime())
                .count();
    }

    public void removeByRoomId(Long roomId, List<Long> uidList) {
        if(CollectionUtil.isEmpty(uidList)){
            return;
        }
        LambdaQueryWrapper<Contact> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Contact::getRoomId, roomId);
        wrapper.in(Contact::getUid, uidList);
        this.remove(wrapper);
    }
}
